package com.demohomework.homeworkdemo.controller;

import com.demohomework.homeworkdemo.pojo.Result;
import com.demohomework.homeworkdemo.pojo.Student;
import com.demohomework.homeworkdemo.pojo.Teacher;
import com.demohomework.homeworkdemo.service.TeacherService;
import com.demohomework.homeworkdemo.util.JwtUtil;
import com.demohomework.homeworkdemo.util.Md5Util;
import com.demohomework.homeworkdemo.util.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    //添加教师
    @PostMapping("/add")
    public Result add(@RequestBody @Validated Teacher teacher) {
        teacherService.add(teacher);
        return Result.success();
    }

    //根据id查询教师
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Integer id) {
        Teacher teacher = teacherService.get(id);
        return Result.success(teacher);
    }
    //注册
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "\\S{2,15}$") String username, @Pattern(regexp = "\\S{5,10}$") String password) {
        //查询用户
        Teacher u = teacherService.findbyUsername(username);
        if (u==null){
            //注册用户
            teacherService.register(username, password);
            return Result.success();
        }else{
            //用户已存在
            return Result.error("用户名已存在");
        }
    }


    //登录
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "\\S{2,10}$") String username, @Pattern(regexp = "\\S{5,10}$") String password) {
        //查询用户
        Teacher loginUser = teacherService.findbyUsername(username);
        if (loginUser!=null){
            //验证密码
            if (Md5Util.string2MD5(password).equals(loginUser.getPassword())){
                //登录成功
                Map<String, Object> claims =new HashMap<>();
                claims.put("id",loginUser.getId());
                claims.put("username",loginUser.getUsername());
                //生成jwt token令牌
                String token = JwtUtil.genToken(claims);
                return Result.success(token);
            }else{
                //密码错误
                return Result.error("密码错误");
            }
        }else{
            //用户不存在
            return Result.error("用户不存在");
        }
    }
    //所有教师信息
    @GetMapping
    public Result<List<Teacher>> list() {
        List<Teacher> teachers = teacherService.list();
        return Result.success(teachers);
    }


    //用户信息
    @GetMapping("/teacherInfo")
    public Result<Teacher> teacherInfo() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        Teacher teacher = teacherService.findbyUsername(username);
        return Result.success(teacher);
    }
    //更新用户信息
    @PutMapping("/update")
    public Result update(@RequestBody @Validated Teacher teacher ) {
        teacherService.update(teacher);
        return Result.success();
    }

    //修改密码
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String, String> params){
        //校验参数
        String oldPwd = (String) params.get("oldPwd");
        String newPwd = (String) params.get("newPwd");
        String rePwd = (String) params.get("rePwd");

        if(!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)){
            return Result.error("缺少必要的参数");
        }

        //原密码验证
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Teacher loginTeacher = teacherService.findbyUsername(username);
        if (!Md5Util.string2MD5(oldPwd).equals(loginTeacher.getPassword())){
            return Result.error("原密码错误");
        }
        //新密码验证
        if (!newPwd.equals(rePwd)){
            return Result.error("两次密码不一致");
        }
        //修改密码
        teacherService.updatePwd(newPwd);
        return Result.success();
    }
    //删除教师
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        teacherService.delete(id);
        return Result.success();
    }
}
